home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / plot1d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  6.6 KB  |  263 lines

  1. /* cat > headers/plot1d.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* plot1d.h: header for plot1d.c file            */
  4. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  5. /* SCCS information: %W%    %G% - NCSA */
  6.  
  7. #define plot1d_h    1
  8.  
  9. #include "all.h"
  10. #include "newext.h"
  11.  
  12.     static int last_x1,last_x2,last_y1,last_y2;
  13.  
  14. /* EOF */
  15. /* cat > src+obj/plot1d/plot_linear.c << "EOF" */
  16. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  17. /* plot_linear: routine that does xy plot        */
  18. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  19. /* SCCS information: %W%    %G% - NCSA */
  20.  
  21. /* #include "plot1d.h" */
  22.  
  23. void
  24. plot_linear ()
  25. {
  26.     float           a, b, xscale, yscale, gvalue, gbound, glow;
  27.     float           userx, usery, userx1, userx2, distance;
  28.     int             cval, xdim, ydim, start;
  29.     int             qx1, qx2, qy1, qy2, x, y;
  30.     int             lastw = curr_image.xdim, lasth = curr_image.ydim;
  31.     int             lastx = curr_image.startx, lasty = curr_image.starty;
  32.     int             x1 = line_rec.x1, x2 = line_rec.x2;
  33.     int             y1 = line_rec.y1, y2 = line_rec.y2;
  34.     float           uxdim = stats_xmax - stats_xmin;
  35.     float           uydim = stats_ymax - stats_ymin;
  36.     Pixwin         *plotpw;
  37.     char            label[MAXNAMELEN];
  38.  
  39.     if (lastw == 0 || lasth == 0)
  40.         return;
  41.  
  42.         /* check line length */
  43.     xdim = ABS (x2 - x1);
  44.     ydim = ABS (y2 - y1);
  45.     if (xdim == 0 && ydim == 0)
  46.     {
  47.         gvalue = get_gvalue ((int) pw_get (pw, x1, y1));
  48.  
  49.             /* apply scaling information */
  50.         userx = (float) (stats_xmin + uxdim * (x1 - lastx) / (float) lastw);
  51.         usery = (float) (stats_ymax - uydim * (y1 - lasty) / (float) lasth);
  52.         sprintf (msgstr, "Note: XY plot - p = (%.2f,%.2f), gvalue = %.2f",
  53.              userx, usery, gvalue);
  54.         msg_write (msgstr);
  55.         last_dir = 0;
  56.         return;
  57.     }
  58.  
  59.     plotpw = canvas_pixwin (pcanvas);
  60.     if (linear_scaling)
  61.     {
  62.         gbound = stats_gmax - stats_gmin;
  63.         glow = stats_gmin;
  64.     }
  65.     else
  66.     {        /* log scale */
  67.         gminexp = (int) floor (log10 ((double) stats_gmin));
  68.         gmaxexp = (int) ceil (log10 ((double) stats_gmax));
  69.         process_gexp (&gminexp, &gmaxexp);
  70.         gbound = (float) (gmaxexp - gminexp);
  71.         glow = gminexp;
  72.     }
  73.     yscale = YPLENGTH / gbound;
  74.  
  75.         /* horizontal line: ydim=0 */
  76.     if (ydim == 0)
  77.     {
  78.         if (x1 > x2)
  79.         {
  80.             x1--;
  81.             x = x1;
  82.             x1 = x2;
  83.             x2 = x;
  84.         }
  85.         else
  86.         {
  87.             x1++;
  88.         }
  89.         xdim--;
  90.         if (last_dir != 1)
  91.         {
  92.             clear_plot ();
  93.             if (has_xscale)
  94.                 strncpy (label, "(X)", MAXNAMELEN - 1);
  95.             else
  96.                 strncpy (label, "(Pixel X)", MAXNAMELEN - 1);
  97.             userx1 = (float) (stats_xmin + uxdim * (x1 - lastx) / (float) lastw);
  98.             userx2 = (float) (stats_xmin + uxdim * (x2 - lastx) / (float) lastw);
  99.             plot_axes (label, userx1, userx2);
  100.             xscale = XPLENGTH / (float) xdim;
  101.             last_x1 = start = x1;
  102.             last_x2 = x2;
  103.             last_dir = 1;
  104.         }
  105.         else
  106.         {
  107.             xscale = last_xscale;
  108.             yscale = last_yscale;
  109.             start = last_x1;
  110.             if (x1 < last_x1)
  111.                 x1 = last_x1;
  112.             if (x2 > last_x2)
  113.                 x2 = last_x2;
  114.         }
  115.  
  116.         cval = pw_get (pw, x1, y1);
  117.         gvalue = get_gvalue (255 - cval);
  118.         qy1 = (int) (YPLOT_MAX - (gvalue - glow) * yscale);
  119.         qx1 = (int) (XPLOT_MIN + (x1 - start) * xscale);
  120.         for (x = x1 + 1; x <= x2; x++)
  121.         {
  122.             cval = pw_get (pw, x, y1);
  123.             gvalue = get_gvalue (255 - cval);
  124.             qy2 = (int) (YPLOT_MAX - (gvalue - glow) * yscale);
  125.             qx2 = (int) (XPLOT_MIN + xscale * (x - start));
  126.             pwVector (plotpw, qx1, qy1, qx2, qy2, PIX_SRC, 255);
  127.             qx1 = qx2;
  128.             qy1 = qy2;
  129.         }
  130.         last_xscale = xscale;
  131.         last_yscale = yscale;
  132.         return;
  133.     }
  134.  
  135.         /* vertical line: xdim=0 */
  136.     if (xdim == 0)
  137.     {
  138.         if (y1 > y2)
  139.         {
  140.             y1--;
  141.             y = y1;
  142.             y1 = y2;
  143.             y2 = y;
  144.         }
  145.         else
  146.         {
  147.             y1++;
  148.         }
  149.         ydim--;
  150.         if (last_dir != 2)
  151.         {
  152.             clear_plot ();
  153.             userx1 = (float) (uydim * (y1 - lasty) / (float) lasth);
  154.             userx2 = (float) (uydim * (y2 - lasty) / (float) lasth);
  155.             userx1 = (float) stats_ymax - userx1;
  156.             userx2 = (float) stats_ymax - userx2;
  157.             if (has_yscale)
  158.                 strncpy (label, "(Y)", MAXNAMELEN - 1);
  159.             else
  160.                 strncpy (label, "(Pixel Y)", MAXNAMELEN - 1);
  161.             plot_axes (label, userx2, userx1);
  162.             xscale = XPLENGTH / (float) ydim;
  163.             last_y2 = start = y2;
  164.             last_y1 = y1;
  165.             last_dir = 2;
  166.         }
  167.         else
  168.         {
  169.             xscale = last_xscale;
  170.             yscale = last_yscale;
  171.             start = last_y2;
  172.             if (y1 < last_y1)
  173.                 y1 = last_y1;
  174.             if (y2 > last_y2)
  175.                 y2 = last_y2;
  176.         }
  177.  
  178.         cval = pw_get (pw, x2, y2);
  179.         gvalue = get_gvalue (255 - cval);
  180.         qy1 = (int) (YPLOT_MAX - (gvalue - glow) * yscale);
  181.         qx1 = (int) (XPLOT_MIN + xscale * (start - y2));
  182.         for (y = y2 - 1; y >= y1; y--)
  183.         {
  184.             cval = pw_get (pw, x2, y);
  185.             gvalue = get_gvalue (255 - cval);
  186.             qy2 = (int) (YPLOT_MAX - (gvalue - glow) * yscale);
  187.             qx2 = (int) (XPLOT_MIN + xscale * (start - y));
  188.             pwVector (plotpw, qx1, qy1, qx2, qy2, PIX_SRC, 255);
  189.             qx1 = qx2;
  190.             qy1 = qy2;
  191.         }
  192.         last_xscale = xscale;
  193.         last_yscale = yscale;
  194.         return;
  195.     }
  196.  
  197.         /* general straight line: xdim and ydim are not zeros */
  198.     clear_plot ();
  199.  
  200.     if (x1 > x2)
  201.     {
  202.         x1--;
  203.         x = x1;
  204.         x1 = x2;
  205.         x2 = x;
  206.         y = y1;
  207.         y1 = y2;
  208.         y2 = y;
  209.     }
  210.     else
  211.         x1++;
  212.     xdim--;
  213.     ydim--;
  214.  
  215.     userx1 = (float) (stats_xmin + uxdim * (x1 - lastx) / (float) lastw);
  216.     userx2 = (float) (stats_xmin + uxdim * (x2 - lastx) / (float) lastw);
  217.     userx = ABS (userx1 - userx2);
  218.     userx1 = (float) (stats_ymax - uydim * (y1 - lasty) / (float) lasth);
  219.     userx2 = (float) (stats_ymax - uydim * (y2 - lasty) / (float) lasth);
  220.     usery = ABS (userx1 - userx2);
  221.     distance = (float) sqrt ((double) (userx * userx + usery * usery));
  222.     if (has_xscale)
  223.         strncpy (label, "(Scaled Distance)", MAXNAMELEN - 1);
  224.     else
  225.         strncpy (label, "(Scaled Pixel Distance)", MAXNAMELEN - 1);
  226.     plot_axes (label, 0., distance);
  227.  
  228.     distance = (float) sqrt ((double) (xdim * xdim + ydim * ydim));
  229.     xscale = XPLENGTH / (float) distance;
  230.     get_eqn (x1, y1, x2, y2, &a, &b);
  231.     y1 = a * x1 + b;
  232.  
  233.     plotpw = canvas_pixwin (pcanvas);
  234.     pw_vector (pw, line_rec.x1, line_rec.y1, line_rec.x2, line_rec.y2,
  235.            PIX_NOT (PIX_DST), 255);
  236.     gvalue = get_gvalue ((int) pw_get (pw, x1, y1));
  237.     pw_vector (pw, line_rec.x1, line_rec.y1, line_rec.x2, line_rec.y2,
  238.            PIX_NOT (PIX_DST), 255);
  239.     qy1 = (int) (YPLOT_MAX - (gvalue - glow) * yscale);
  240.     qx1 = XPLOT_MIN;
  241.     for (x = x1 + 1; x <= x2; x++)
  242.     {
  243.         y = (int) (a * x + b);
  244.         pw_vector (pw, line_rec.x1, line_rec.y1, line_rec.x2, line_rec.y2,
  245.                PIX_NOT (PIX_DST), 255);
  246.         cval = pw_get (pw, x, y);
  247.         gvalue = get_gvalue (cval);
  248.         pw_vector (pw, line_rec.x1, line_rec.y1, line_rec.x2, line_rec.y2,
  249.                PIX_NOT (PIX_DST), 255);
  250.         qy2 = (int) (YPLOT_MAX - (gvalue - glow) * yscale);
  251.         distance = (float) sqrt ((double) ((x - x1) * (x - x1) + (y - y1) * (y - y1)));
  252.         qx2 = (int) (XPLOT_MIN + xscale * distance);
  253.         pwVector (plotpw, qx1, qy1, qx2, qy2, PIX_SRC, 255);
  254.         qx1 = qx2;
  255.         qy1 = qy2;
  256.     }
  257.     last_dir = 3;
  258.     last_xscale = xscale;
  259.     last_yscale = yscale;
  260.     return;
  261. }
  262. /* EOF */
  263.